#include "StdAfx.h"

#include "ColorUtils/Color.h"
#include "ColorUtils/Image.h"

#include "FirstIntersectionResult.h"
#include "SecondIntersectionResult.h"
#include "Intersectable.h"
#include "Light.h"
#include "Scene.h"
#include "World.h"


#include "PointLight.h"

PointLight::PointLight(Scene_ptr scene, const Point& position, const Point& attenuation, const Color& intensity) 
	: _scene(scene), _position(position), _attenuation(attenuation), _intensity(intensity)
{}

Ray PointLight::IntensityAtPoint(const Point& point)
{
	Point pointCopy(point);
	pointCopy -= _position;
	double distence;
	SecondIntersectionResult tempResult = SecondIntersectionResult();
	
	_scene->IntersectionFinder->WhereIntersects(Ray(_position, pointCopy), tempResult);
	if (abs(pointCopy.Length() - (distence = tempResult.Distance)) < 0.001)
		return Ray(Point(_intensity / _attenuation.ScalarProduct(Point(1, distence, distence * distence))), _position - point);

	return Ray(Point(0, 0, 0), _position - point);
}

PointLight::~PointLight() {};